package com.hao.statussheet.service;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.annotation.Nullable;

import com.hao.statussheet.bean.MusicBean;
import com.hao.statussheet.handler.ActivitySendService;
import com.hao.statussheet.handler.ServiceSendActivityHandler;
import com.hao.statussheet.utils.PalyListUtils;
import com.hao.statussheet.utils.PalyStatus;
import com.hao.statussheet.utils.PlayerUtils;

/**
 * Created by hao on 16/12/4.
 */
public class MyMusicService extends Service {

    private static final String TAG = MyMusicService.class.getSimpleName();

    private static final int TIMES = 10;

    private PlayerUtils playerUtils;

    private int nowPosition = -1;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        playerUtils = new PlayerUtils();
        //播放自动结束的处理
        playerUtils.setOnPlayStopListener(onPlayStopListener);
        //播放准备中,即播放马上就要开始的状态
        playerUtils.setOnPlayStartListener(onPlayStartListener);
        ActivitySendService.setOnMusicOperationListener(new ActivitySendService.OnMusicOperationListener() {
            @Override
            public void onPause() {
                playerUtils.pause();
            }

            @Override
            public void onStart() {
                playerUtils.play();
            }

            @Override
            public void onSwith(boolean isLifeOrReight) {
                playerUtils.life0rRieght(isLifeOrReight);
            }

            @Override
            public void onStop() {
                playerUtils.stop();
            }

            @Override
            public void onSeekTo(int seek) {
                playerUtils.seekTo(seek);
            }

            @Override
            public void onSwithAll() {

            }
        });
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        MusicBean musicBean = PalyListUtils.getMusicBeanList().get(0);
        musicBean.setStatus(PalyStatus.PROGRESS);
        playerUtils.setDataSource(musicBean);
        playerUtils.play();
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        playerUtils.stop();
    }

    /**
     * 播放自动结束的处理
     */
    private PlayerUtils.OnPlayStopListener onPlayStopListener = new PlayerUtils.OnPlayStopListener() {
        @Override
        public void onPlayStop() {
            onDestroy();
        }
    };
    /**
     * 播放开始的回调
     */
    private PlayerUtils.OnPlayStartListener onPlayStartListener = new PlayerUtils.OnPlayStartListener() {
        @Override
        public void onPlayStart() {
            Message message = handlerTime.obtainMessage();
            if (message == null)
                message = new Message();
            message.what = TIMES;
            handlerTime.sendMessageDelayed(message, 100);
        }
    };

    private Handler handlerTime = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case TIMES:
                    if (playerUtils == null || !playerUtils.isPlaying())
                        return;

                    String time = playerUtils.getPlayTime();
                    int position = playerUtils.getPlayCurrent();
                    Message sendMessage = ServiceSendActivityHandler.obtainMessage();
                    sendMessage.what = ServiceSendActivityHandler.ON_TIME;
                    sendMessage.obj = time;
                    ServiceSendActivityHandler.getHandler().sendMessage(sendMessage);
                    Message sendMessage2 = ServiceSendActivityHandler.obtainMessage();
                    sendMessage2.what = ServiceSendActivityHandler.ON_UPDATE;
                    sendMessage2.obj = position;
                    ServiceSendActivityHandler.getHandler().sendMessage(sendMessage2);

                    Message message = handlerTime.obtainMessage();
                    if (message == null)
                        message = new Message();
                    message.what = TIMES;
                    handlerTime.sendMessageDelayed(message, 1000);
                    break;
                default:
                    break;
            }
        }
    };


}
